package com.sikadwafastdaisfondturbo.pretloan.app

import com.sikadwafastdaisfondturbo.pretloan.analysis.TestAnalysisInit
import com.sikadwafastdaisfondturbo.pretloan.base.BaseApp
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.OkHttpLoader
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


/**
 * Sikadwa
 * @Author：wwdeng
 * @CreateTime：2024/4/14 19:27
 * <pre>
 *
 * </pre>
 */
class App: BaseApp() {

    companion object {

        private var instance: App by NotNullSingleValueVar()
        fun  instance() = instance
        /**
         * 获取当前手机屏幕的宽
         */
        fun getPhoneWidth(): Int {
            // 手机屏幕的宽
            // 获取DisplayMetrics对象
            val displayMetrics = instance().resources.displayMetrics
            return displayMetrics.widthPixels
        }

        public fun getPhoneHeight(): Int {
            // 手机屏幕的高
            // 获取DisplayMetrics对象
            val displayMetrics = instance().resources.displayMetrics
            return displayMetrics.heightPixels
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this

        initLibConfigs()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        HttpHelper.init(OkHttpLoader())
        //初始化 analysis
        TestAnalysisInit.initAnalysisLibAtAppLaunch()
    }

    //定义一个属性管理类，进行非空和重复赋值的判断
    private class  NotNullSingleValueVar<T>() : ReadWriteProperty<Any?, T> {
        private var value: T? =null
        //非空的校验
        override fun getValue(thisRef: Any?, property: KProperty<*>): T {
            return value ?: throw IllegalArgumentException("application not initialized")
        }
        //重复赋值的校验
        override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
            this.value=if (this.value==null) value
            else throw IllegalArgumentException("application already initialized")
        }
    }
}